package com.hx.setting.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
public class StringUtils
{

	/** The empty String <code>""</code>.@since 2.0 */
	public static final String	EMPTY			= "";

	/** Represents a failed index search.@since 2.1 */
	public static final int		INDEX_NOT_FOUND	= -1;

	/** <p>The maximum size to which the padding constant(s) can expand.</p> */
	private static final int	PAD_LIMIT		= 8192;

	/** 
	 * <p><code>StringUtils</code> instances should NOT be constructed in
	 * standard programming. Instead, the class should be used as
	 * <code>StringUtils.trim(" foo ");</code>.</p>
	 * 
	 * <p>This constructor is public to permit tools that require a JavaBean
	 * instance to operate.</p>
	 */
	public StringUtils()
	{
		super();
	}

	/** 
	 * <p>Checks if a String is empty ("") or null.</p>
	 * 
	 * <pre>
	 * StringUtils.isEmpty(null)      = true
	 * StringUtils.isEmpty("")        = true
	 * StringUtils.isEmpty(" ")       = false
	 * StringUtils.isEmpty("bob")     = false
	 * StringUtils.isEmpty("  bob  ") = false
	 * </pre>
	 * 
	 * <p>NOTE: This method changed in Lang version 2.0.
	 * It no longer trims the String.
	 * That functionality is available in isBlank().</p>
	 * 
	 * @param str  the String to check, may be null
	 * @return <code>true</code> if the String is empty or null
	 */
	public static boolean isEmpty(String str)
	{
		return str == null || str.length() == 0;
	}

	/** 
	 * <p>Checks if a String is whitespace, empty ("") or null.</p>
	 * 
	 * <pre>
	 * StringUtils.isBlank(null)      = true
	 * StringUtils.isBlank("")        = true
	 * StringUtils.isBlank(" ")       = true
	 * StringUtils.isBlank("bob")     = false
	 * StringUtils.isBlank("  bob  ") = false
	 * </pre>
	 * 
	 * @param str  the String to check, may be null
	 * @return <code>true</code> if the String is null, empty or whitespace
	 * @since 2.0
	 */
	public static boolean isBlank(String str)
	{
		int strLen;
		if ( str == null || (strLen = str.length()) == 0 )
		{
			return true;
		}
		for ( int i = 0 ; i < strLen ; i++ )
		{
			if ( (Character.isWhitespace( str.charAt( i ) ) == false) )
			{
				return false;
			}
		}
		return true;
	}

	/** 
	 * <p>Removes control characters (char &lt;= 32) from both
	 * ends of this String, handling <code>null</code> by returning
	 * an empty String ("").</p>
	 * 
	 * <pre>
	 * StringUtils.clean(null)          = ""
	 * StringUtils.clean("")            = ""
	 * StringUtils.clean("abc")         = "abc"
	 * StringUtils.clean("    abc    ") = "abc"
	 * StringUtils.clean("     ")       = ""
	 * </pre>
	 * 
	 * @see java.lang.String#trim()
	 * @param str  the String to clean, may be null
	 * @return the trimmed text, never <code>null</code>
	 * 
	 */
	public static String clean(String str)
	{
		return str == null ? EMPTY : str.trim();
	}

	/** 
	 * <p>Removes control characters (char &lt;= 32) from both
	 * ends of this String, handling <code>null</code> by returning
	 * <code>null</code>.</p>
	 * 
	 * <p>The String is trimmed using {@link String#trim()}.
	 * Trim removes start and end characters &lt;= 32.
	 * To strip whitespace use {@link #strip(String)}.</p>
	 * 
	 * <p>To trim your choice of characters, use the
	 * {@link #strip(String, String)} methods.</p>
	 * 
	 * <pre>
	 * StringUtils.trim(null)          = null
	 * StringUtils.trim("")            = ""
	 * StringUtils.trim("     ")       = ""
	 * StringUtils.trim("abc")         = "abc"
	 * StringUtils.trim("    abc    ") = "abc"
	 * </pre>
	 * 
	 * @param str  the String to be trimmed, may be null
	 * @return the trimmed string, <code>null</code> if null String input
	 */
	public static String trim(String str)
	{
		return str == null ? null : str.trim();
	}

	/** 
	 * <p>Removes control characters (char &lt;= 32) from both
	 * ends of this String returning an empty String ("") if the String
	 * is empty ("") after the trim or if it is <code>null</code>.
	 * 
	 * <p>The String is trimmed using {@link String#trim()}.
	 * Trim removes start and end characters &lt;= 32.
	 * To strip whitespace use {@link #stripToEmpty(String)}.</p>
	 * 
	 * <pre>
	 * StringUtils.trimToEmpty(null)          = ""
	 * StringUtils.trimToEmpty("")            = ""
	 * StringUtils.trimToEmpty("     ")       = ""
	 * StringUtils.trimToEmpty("abc")         = "abc"
	 * StringUtils.trimToEmpty("    abc    ") = "abc"
	 * </pre>
	 * 
	 * @param str  the String to be trimmed, may be null
	 * @return the trimmed String, or an empty String if <code>null</code> input
	 * @since 2.0
	 */
	public static String trimToEmpty(String str)
	{
		return str == null ? EMPTY : str.trim();
	}

	/** 
	 * <p>Strips whitespace from the start and end of a String  returning
	 * <code>null</code> if the String is empty ("") after the strip.</p>
	 * 
	 * <p>This is similar to {@link #trimToNull(String)} but removes whitespace.
	 * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
	 * 
	 * <pre>
	 * StringUtils.stripToNull(null)     = null
	 * StringUtils.stripToNull("")       = null
	 * StringUtils.stripToNull("   ")    = null
	 * StringUtils.stripToNull("abc")    = "abc"
	 * StringUtils.stripToNull("  abc")  = "abc"
	 * StringUtils.stripToNull("abc  ")  = "abc"
	 * StringUtils.stripToNull(" abc ")  = "abc"
	 * StringUtils.stripToNull(" ab c ") = "ab c"
	 * </pre>
	 * 
	 * @param str  the String to be stripped, may be null
	 * @return the stripped String,
	 * <code>null</code> if whitespace, empty or null String input
	 * @since 2.0
	 */
	public static String stripToNull(String str)
	{
		if ( str == null )
		{
			return null;
		}
		str = strip( str ,null );
		return str.length() == 0 ? null : str;
	}

	/** 
	 * <p>Strips whitespace from the start and end of a String  returning
	 * an empty String if <code>null</code> input.</p>
	 * 
	 * <p>This is similar to {@link #trimToEmpty(String)} but removes whitespace.
	 * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
	 * 
	 * <pre>
	 * StringUtils.stripToEmpty(null)     = ""
	 * StringUtils.stripToEmpty("")       = ""
	 * StringUtils.stripToEmpty("   ")    = ""
	 * StringUtils.stripToEmpty("abc")    = "abc"
	 * StringUtils.stripToEmpty("  abc")  = "abc"
	 * StringUtils.stripToEmpty("abc  ")  = "abc"
	 * StringUtils.stripToEmpty(" abc ")  = "abc"
	 * StringUtils.stripToEmpty(" ab c ") = "ab c"
	 * </pre>
	 * 
	 * @param str  the String to be stripped, may be null
	 * @return the trimmed String, or an empty String if <code>null</code> input
	 * @since 2.0
	 */
	public static String stripToEmpty(String str)
	{
		return str == null ? EMPTY : strip( str ,null );
	}

	/** 
	 * <p>Strips any of a set of characters from the start and end of a String.
	 * This is similar to {@link String#trim()} but allows the characters
	 * to be stripped to be controlled.</p>
	 * 
	 * <p>A <code>null</code> input String returns <code>null</code>.
	 * An empty string ("") input returns the empty string.</p>
	 * 
	 * <p>If the stripChars String is <code>null</code>, whitespace is
	 * stripped as defined by {@link Character#isWhitespace(char)}.
	 * Alternatively use {@link #strip(String)}.</p>
	 * 
	 * <pre>
	 * StringUtils.strip(null, *)          = null
	 * StringUtils.strip("", *)            = ""
	 * StringUtils.strip("abc", null)      = "abc"
	 * StringUtils.strip("  abc", null)    = "abc"
	 * StringUtils.strip("abc  ", null)    = "abc"
	 * StringUtils.strip(" abc ", null)    = "abc"
	 * StringUtils.strip("  abcyx", "xyz") = "  abc"
	 * </pre>
	 * 
	 * @param str  the String to remove characters from, may be null
	 * @param stripChars  the characters to remove, null treated as whitespace
	 * @return the stripped String, <code>null</code> if null String input
	 */
	public static String strip(String str, String stripChars)
	{
		if ( isEmpty( str ) )
		{
			return str;
		}
		str = stripStart( str ,stripChars );
		return stripEnd( str ,stripChars );
	}

	/** 
	 * <p>Strips any of a set of characters from the start of a String.</p>
	 * 
	 * <p>A <code>null</code> input String returns <code>null</code>.
	 * An empty string ("") input returns the empty string.</p>
	 * 
	 * <p>If the stripChars String is <code>null</code>, whitespace is
	 * stripped as defined by {@link Character#isWhitespace(char)}.</p>
	 * 
	 * <pre>
	 * StringUtils.stripStart(null, *)          = null
	 * StringUtils.stripStart("", *)            = ""
	 * StringUtils.stripStart("abc", "")        = "abc"
	 * StringUtils.stripStart("abc", null)      = "abc"
	 * StringUtils.stripStart("  abc", null)    = "abc"
	 * StringUtils.stripStart("abc  ", null)    = "abc  "
	 * StringUtils.stripStart(" abc ", null)    = "abc "
	 * StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
	 * </pre>
	 * 
	 * @param str  the String to remove characters from, may be null
	 * @param stripChars  the characters to remove, null treated as whitespace
	 * @return the stripped String, <code>null</code> if null String input
	 */
	public static String stripStart(String str, String stripChars)
	{
		int strLen;
		if ( str == null || (strLen = str.length()) == 0 )
		{
			return str;
		}
		int start = 0;
		if ( stripChars == null )
		{
			while ( (start != strLen) && Character.isWhitespace( str.charAt( start ) ) )
			{
				start++;
			}
		}
		else if ( stripChars.length() == 0 )
		{
			return str;
		}
		else
		{
			while ( (start != strLen) && (stripChars.indexOf( str.charAt( start ) ) != INDEX_NOT_FOUND) )
			{
				start++;
			}
		}
		return str.substring( start );
	}

	/** 
	 * <p>Strips any of a set of characters from the end of a String.</p>
	 * 
	 * <p>A <code>null</code> input String returns <code>null</code>.
	 * An empty string ("") input returns the empty string.</p>
	 * 
	 * <p>If the stripChars String is <code>null</code>, whitespace is
	 * stripped as defined by {@link Character#isWhitespace(char)}.</p>
	 * 
	 * <pre>
	 * StringUtils.stripEnd(null, *)          = null
	 * StringUtils.stripEnd("", *)            = ""
	 * StringUtils.stripEnd("abc", "")        = "abc"
	 * StringUtils.stripEnd("abc", null)      = "abc"
	 * StringUtils.stripEnd("  abc", null)    = "  abc"
	 * StringUtils.stripEnd("abc  ", null)    = "abc"
	 * StringUtils.stripEnd(" abc ", null)    = " abc"
	 * StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
	 * StringUtils.stripEnd("120.00", ".0")   = "12"
	 * </pre>
	 * 
	 * @param str  the String to remove characters from, may be null
	 * @param stripChars  the set of characters to remove, null treated as whitespace
	 * @return the stripped String, <code>null</code> if null String input
	 */
	public static String stripEnd(String str, String stripChars)
	{
		int end;
		if ( str == null || (end = str.length()) == 0 )
		{
			return str;
		}
		if ( stripChars == null )
		{
			while ( (end != 0) && Character.isWhitespace( str.charAt( end - 1 ) ) )
			{
				end--;
			}
		}
		else if ( stripChars.length() == 0 )
		{
			return str;
		}
		else
		{
			while ( (end != 0) && (stripChars.indexOf( str.charAt( end - 1 ) ) != INDEX_NOT_FOUND) )
			{
				end--;
			}
		}
		return str.substring( 0 ,end );
	}

	/** 
	 * 是否是true字符串
	 * @param str
	 * @return
	 */
	public static boolean isTrue(String str)
	{
		return "true".equalsIgnoreCase( str ) || "t".equalsIgnoreCase( str ) || "1".equals( str ) || "yes".equalsIgnoreCase( str ) || "on".equalsIgnoreCase( str );
	}

	/** 
	 * 删除空白字符串
	 * @param str
	 * @return
	 */
	public static String deleteWhitespace(String str)
	{
		if ( str == null )
		{
			return null;
		}
		int sz = str.length();
		StringBuilder buffer = new StringBuilder( sz );
		for ( int i = 0 ; i < sz ; i++ )
		{
			if ( !Character.isWhitespace( str.charAt( i ) ) )
			{
				buffer.append( str.charAt( i ) );
			}
		}
		return buffer.toString();
	}

	public static Object[] join(Object[] src, Object[] dis)
	{
		Object[] answer = new Object[src.length + dis.length];
		System.arraycopy( src ,0 ,answer ,0 ,src.length );
		System.arraycopy( dis ,0 ,answer ,src.length ,dis.length );
		return answer;
	}

	/** 
	 * 分割字符串按照 \n|;|,|\\|
	 * @param str
	 * @return
	 */
	public static String[] parseStringsplit(String str)
	{
		String[] answer = null;
		if ( str != null && str.length() > 0 )
		{
			String s[] = str.split( "\n|;|,|\\|" );
			List<String> list = new ArrayList<String>();
			if ( s.length > 0 )
			{
				for ( String t : s )
				{
					if ( t == null || t.length() == 0 ) continue;
					t = t.trim();
					if ( t.length() == 0 ) continue;
					list.add( t );
				}
				answer = list.toArray( new String[list.size()] );
			}
			else
			{
				answer = null;
			}
		}
		return answer;
	}

	/** 
	 * 把InputStream转换成String
	 * 
	 * @param in 输入流
	 * @param charset
	 * 字符集,默认使用utf-8
	 * @return
	 * @throws IOException
	 */
	public static String streamToString(InputStream in, String charset) throws IOException
	{
		StringBuilder answer = new StringBuilder();
		BufferedReader reader = new BufferedReader( new InputStreamReader( in, charset == null ? "utf-8" : charset ) );
		String line = null;
		while ( (line = reader.readLine()) != null )
		{
			answer.append( line + "\n" );
		}
		return answer.toString();
	}
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:B465164651516443f6a294070fb314673 (do not edit this line) */
